package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.domain.User;
import com.itheima.dto.LoginDto;
import com.itheima.service.UserService;
import com.itheima.utils.RedisKeys;
import com.itheima.utils.SendSms;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/sendMsg")
    public R sendMsg(@RequestBody User user, HttpServletRequest request){
        //生成一个验证码
        Integer code = ValidateCodeUtils.generateValidateCode(6);
        System.out.println("生成的验证码为:"+code);
        //发送给指定的手机
        /*SendSms.sendMsg(user.getPhone(),code+"");*/
        //把验证码存储到session中
        HttpSession session = request.getSession();
        /*session.setAttribute(user.getPhone(),code+"");*/
        redisTemplate.opsForValue().set(RedisKeys.phone+user.getPhone(),code+"",5,TimeUnit.MINUTES);
        return R.success("手机验证码短信发送成功");
    }

    @PostMapping("/login")
    public R login(@RequestBody LoginDto loginDto,HttpServletRequest request){
        //获得手机和验证码
        String phone = loginDto.getPhone();
        String code = loginDto.getCode();
        //获得session中的验证码
        HttpSession session = request.getSession();
        //String codeInSession = (String) session.getAttribute(phone);
        //从Redis中获取缓存的验证码
        String codeInSession  = (String) redisTemplate.opsForValue().get(RedisKeys.phone+phone);
        //如果验证码为空
        if(codeInSession==null){
            return R.error("参数异常");
        }
        //如果验证码不匹配
        if(!code.equals(codeInSession)){
            return R.error("验证码不匹配");
        }
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getPhone,phone);
        User user = userService.getOne(lqw);
        //判断用户是否注册
        if(user==null){
            user=new User();
            user.setPhone(phone);
            user.setStatus(1);
            userService.save(user);
        }
        session.setAttribute("user",user.getId());
        //登录成功,删除redis中的验证码
        redisTemplate.delete(RedisKeys.phone+phone);
        return R.success(user);
    }

    @PostMapping("/loginout")
    public R loginOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        return R.success("退出成功");
    }
}
